package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaInvitationDetailDto;
import cn.chinaflame.pagodas.dto.PaInvitationDto;
import cn.chinaflame.pagodas.dto.VwBlessInvitationDto;
import cn.chinaflame.pagodas.entity.VwBlessBeinvited;
import cn.chinaflame.pagodas.req.PaInvitationReq;
import cn.chinaflame.pagodas.service.PaInvitationService;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 邀请接口
 *
 * @author bm
 * 2018年1月13日 上午10:50:15
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/invitation/")
public class OpenPaInvitationAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenPaInvitationAPI.class);

    @Autowired
    private PaInvitationService paInvitationService;

    /**
     * 查询我的邀请
     *
     * @param memberId 会员id
     * @param openid   openid
     * @return
     */
    @GetMapping("query/list")
    @ApiDoc(PaInvitationDto.class)
    public Object query(String memberId, String openid) {
        log.info("查询邀请列表memberId:{},openid:{}", memberId, openid);
        List<PaInvitationDto> dtos = paInvitationService.query(memberId, openid);
        return Result.create(dtos);
    }

    /**
     * 邀请详情
     *
     * @param memberId     会员id
     * @param openid       openid
     * @param mobile       会员手机号
     * @param invitationId 邀请id
     * @return
     */
    @GetMapping("query/list/detail")
    @ApiDoc(VwBlessInvitationDto.class)
    public Object queryDetail(String memberId, String openid, String mobile, String invitationId) {
        log.info("查询邀请详情列表memberId:{},openid:{},mobile:{},invitationId:{}", memberId, openid, mobile, invitationId);
        return Result.create(paInvitationService.queryDetail2(memberId, openid, mobile, invitationId));
    }

    /**
     * 被邀请信息
     *
     * @param memberId 会员id
     * @param openid   openid
     * @param mobile   手机号
     * @return
     */
    @GetMapping("query/list/beinvited/detail")
    @ApiDoc(VwBlessBeinvited.class)
    public Object queryBeinvitedDetail(String memberId, String openid, String mobile) {
        log.info("查询邀请详情列表memberId:{},openid:{},mobile:{},invitationId:{}", memberId, openid, mobile);
        return Result.create(paInvitationService.queryBeinvitedDetail2(memberId, openid, mobile));
    }

    /**
     * 邀请信息统计
     *
     * @param memberId     会员id
     * @param openid       openid
     * @param invitationId 邀请id
     * @return
     */
    @GetMapping("query/list/detail/statistic")
    @ApiDoc(Result.class)
    public Object queryDetailStatistic(String memberId, String openid, String invitationId) {
        log.info("查询邀请统计memberId:{},openid:{},invitationId:{}",
                memberId, openid, invitationId);
        if (!Strings.isNullOrEmpty(invitationId))
            return Result.create(paInvitationService.queryDetailByStatisticByInvitationId(invitationId));
        return Result.create(paInvitationService.queryDetailByStatisticByOpenid(openid));
    }

    /**
     * 添加邀请
     *
     * @param req
     * @return
     */
    @PostMapping("add")
    @ApiDoc(value = PaInvitationReq.class, result = Result.class)
    public Object add(@RequestBody PaInvitationReq req) {
        log.info("添加邀请信息req:{}", req);
        return paInvitationService.add(req);
    }

    /**
     * 邀请详情
     *
     * @param invitationId 邀请id
     * @return
     */
    @GetMapping("query/detail")
    @ApiDoc(PaInvitationDetailDto.class)
    public Object query(String invitationId) {
        log.info("查询邀请详情invitationId:{}", invitationId);
        return Result.create(paInvitationService.queryById(invitationId));
    }

}





